package com.ruoyi.biz.controller;

import com.ruoyi.biz.dto.LotteryDTO;
import com.ruoyi.biz.dto.LotteryResultDTO;
import com.ruoyi.biz.entity.LotteryDO;
import com.ruoyi.biz.entity.LotteryResultDO;
import com.ruoyi.biz.entity.WechatEntity;
import com.ruoyi.biz.service.LotteryResultService;
import com.ruoyi.biz.service.LotteryService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.util.ValidateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/biz/lottery")
public class LotteryController extends BaseController {

    private String prefix = "lottery";

    @Resource
    LotteryService lotteryService;
    @Resource
    LotteryResultService lotteryResultService;

    @GetMapping()
    public String build() {
        return prefix + "/index";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LotteryDTO lotteryDTO, HttpServletRequest httpServletRequest) {
        startPage();
        List<LotteryDO> list = lotteryService.queryList(lotteryDTO);
        return getDataTable(list);
    }

    /**
     * 新增用户
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    @Log(title = "抽奖活动管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LotteryDTO lotteryDTO) {
        if (ValidateUtil.isEmpty(lotteryDTO)) {
            return error("新增抽奖活动的入参不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryDTO.getLotteryTitle())) {
            return error("新增抽奖活动的抽奖活动标题不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryDTO.getLotteryContent())) {
            return error("新增抽奖活动的抽奖活动内容不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryDTO.getBeginTime())) {
            return error("新增抽奖活动的开始时间不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryDTO.getEndTime())) {
            return error("新增抽奖活动的结束时间不能为空！");
        }
        if (lotteryDTO.getBeginTime().compareTo(lotteryDTO.getEndTime()) > 0) {
            return error("抽奖活动的结束时间不能比开始时间早！");
        }
        //判断和其他抽奖活动是否有时间交叉
        LotteryDTO qLotteryDTO = new LotteryDTO();
        qLotteryDTO.setQueryDate(lotteryDTO.getBeginTime());
        qLotteryDTO.setValiFlag("1");
        List<LotteryDO> list = lotteryService.queryList(qLotteryDTO);
        if (!ValidateUtil.isEmpty(list)) {
            return error("抽奖活动的开始时间与其他抽奖活动存在时间交叉！");
        }
        qLotteryDTO.setQueryDate(lotteryDTO.getEndTime());
        list = lotteryService.queryList(qLotteryDTO);
        if (!ValidateUtil.isEmpty(list)) {
            return error("抽奖活动的结束时间与其他抽奖活动存在时间交叉！");
        }

        lotteryDTO.setCrterId(getSysUser().getUserId().toString());
        lotteryDTO.setCrterName(getSysUser().getUserName());
        lotteryDTO.setCrteTime(new Date());
        lotteryDTO.setOpterId(getSysUser().getUserId().toString());
        lotteryDTO.setOpterName(getSysUser().getUserName());
        lotteryDTO.setOptTime(new Date());
        return toAjax(lotteryService.insertLottery(lotteryDTO));
    }

    /**
     * 修改抽奖活动信息
     */
    @GetMapping("/edit/{lotteryId}")
    public String edit(@PathVariable("lotteryId") String lotteryId, ModelMap mmap) {
        mmap.put("lottery", lotteryService.queryById(lotteryId));
        return prefix + "/edit";
    }

    @Log(title = "抽奖活动管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LotteryDTO lotteryDTO) {
        if (ValidateUtil.isEmpty(lotteryDTO)) {
            return error("修改抽奖活动的入参不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryDTO.getLotteryId())) {
            return error("修改抽奖活动的抽奖活动ID不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryDTO.getLotteryTitle())) {
            return error("修改抽奖活动的抽奖活动标题不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryDTO.getLotteryContent())) {
            return error("修改抽奖活动的抽奖活动内容不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryDTO.getBeginTime())) {
            return error("修改抽奖活动的开始时间不能为空！");
        }
        if (ValidateUtil.isEmpty(lotteryDTO.getEndTime())) {
            return error("修改抽奖活动的结束时间不能为空！");
        }
        if (lotteryDTO.getBeginTime().compareTo(lotteryDTO.getEndTime()) > 0) {
            return error("抽奖活动的结束时间不能比开始时间早！");
        }

        //判断和其他抽奖活动是否有时间交叉
        LotteryDTO qLotteryDTO = new LotteryDTO();
        qLotteryDTO.setQueryDate(lotteryDTO.getBeginTime());
        qLotteryDTO.setValiFlag("1");
        List<LotteryDO> list = lotteryService.queryList(qLotteryDTO);
        if (!ValidateUtil.isEmpty(list)) {
            //排除自己
            list = list.stream().filter(item -> !lotteryDTO.getLotteryId().equals(item.getLotteryId())).collect(Collectors.toList());
            if (!ValidateUtil.isEmpty(list)) {
                return error("抽奖活动的开始时间与其他抽奖活动存在时间交叉！");
            }
        }
        qLotteryDTO.setQueryDate(lotteryDTO.getEndTime());
        list = lotteryService.queryList(qLotteryDTO);
        if (!ValidateUtil.isEmpty(list)) {
            //排除自己
            list = list.stream().filter(item -> !lotteryDTO.getLotteryId().equals(item.getLotteryId())).collect(Collectors.toList());
            if (!ValidateUtil.isEmpty(list)) {
                return error("抽奖活动的结束时间与其他抽奖活动存在时间交叉！");
            }
        }

        lotteryDTO.setOpterId(getSysUser().getUserId().toString());
        lotteryDTO.setOpterName(getSysUser().getUserName());
        lotteryDTO.setOptTime(new Date());
        return toAjax(lotteryService.updateLottery(lotteryDTO));
    }

    @Log(title = "抽奖活动管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        LotteryResultDTO qLotteryResultDTO = new LotteryResultDTO();
        qLotteryResultDTO.setLotteryId(ids);
        List<LotteryResultDO> lotteryResultDTOS = lotteryResultService.queryList(qLotteryResultDTO);
        if (!ValidateUtil.isEmpty(lotteryResultDTOS)) {
            return error("本次抽奖活动已经存在中奖结果，无法删除！");
        }
        return toAjax(lotteryService.deleteLottery(ids));
    }
}
